<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title></title>
		<script src="__INS__/js/plugins/workerman/swfobject.js"></script>
		<script src="__INS__/js/plugins/workerman/web_socket.js"></script>
		<script src="__INS__/js/plugins/workerman/json.js"></script>
		<script src="__INS__/js/jquery-2.1.1.js"></script>

		<script type="text/javascript">
		
			var reconnet_count=0;
			if ( typeof console == "undefined") {
				this.console = {
					log : function(msg) {
					}
				};
			}
			WEB_SOCKET_SWF_LOCATION = "__INS__/js/plugins/workerman/WebSocketMain.swf";
			WEB_SOCKET_DEBUG = true;
			var ws, name, client_list = {}, timeid, reconnect = false;
			function init() {
				// 创建websocket
				ws = new WebSocket("ws://" + document.domain + ":7272");
				// 当socket连接打开时，输入用户名
				ws.onopen = function() {
					timeid && window.clearInterval(timeid);
					name = "{:get_user_id()}";
					if (reconnect == false) {
						// 登录
						var login_data = JSON.stringify({
							"type" : "login",
							"client_name" : name,
							"room_id" : 1
						});
						console.log("websocket握手成功，发送登录数据:" + login_data);
						ws.send(login_data);
						reconnect = true;
					} else {
						// 断线重连
						var relogin_data = JSON.stringify({
							"type" : "re_login",
							"client_name" : name,
							"room_id" : 1
						});
						console.log("websocket握手成功，发送重连数据:" + relogin_data);
						ws.send(relogin_data);
					}
				};
				// 当有消息时根据消息类型显示不同信息
				ws.onmessage = function(e) {
					console.log(e.data);
					var data = JSON.parse(e.data);
					switch(data['type']) {
					// 服务端ping客户端
					case 'ping':
						ws.send(JSON.stringify({
							"type" : "pong"
						}));
						break;
						;
					case 'say':				
						//window.parent.push_info(data['content']);
						//$hash=data['content'].hash;
						//console.log($hash);
						get_push();						
						break;
					}
				};
				ws.onclose = function() {
					console.log("连接关闭，定时重连"+reconnet_count);
					// 定时重连
					window.clearInterval(timeid);
					reconnet_count++;
					if(reconnet_count<5){
						timeid = window.setInterval(init, 3000);	
					}					
				};
				ws.onerror = function() {
					console.log("出现错误");
				};
			}

			function get_push() {
				$.getJSON("{:U('push/server3')}", function(result){
					if (result.status) {
						$content = eval('(' + result.data.data + ')');
						window.parent.push_info($content);
					}
					if(result.status>0){
						get_push();
					}
				});
			}

			$(function() {
				get_push();
				
				$("#test").click(function() {
					$.getJSON("{:U('test')}", function(result) {
					});
				});
			});
		</script>
	</head>
	<body onload="init();">
		<button id="test">
			test
		</button>
	</body>
</html>
